<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item :to="{ path: '/market/marketingPlay' }"> <i class="el-icon-lx-cascades"></i> 营销玩法 </el-breadcrumb-item>
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 商品推荐 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="page">
            <div class="re-setting">
                <span class="box-top-title">推荐设置</span>
                <div class="settings-content">
                    <span>开启推荐后，将从<b>推荐商品中随机选取最多6个商品</b>，显示在本店铺的【精品推荐】模块</span>
                    <!-- <el-switch v-model="switch_recommend" @change="enable($event)"></el-switch> -->
                    <!-- <el-button type="text">预览效果</el-button> -->
                </div>
            </div>
            <div class="re-setting">
                <span class="box-top-title">推荐商品</span>
                <span class="title_detail">当前数量: {{ pageTotal }} | 商品下架后将不显示在【精品推荐】模块</span>
                <div class="operation">
                    <el-button type="primary" class="handle-del mr10" @click="addDialog">添加推荐商品</el-button>
                    <div class="handle-box">
                        <el-input v-model="query.keyword" placeholder="输入商品名称" class="handle-input mr10"></el-input>
                        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                    </div>
                </div>
                <el-table :data="tableData" class="table" ref="multipleTable" header-cell-class-name="table-header">
                    <el-table-column label="商品">
                        <template slot-scope="scope">
                            <div class="course-info-wrapper">
                                <div class="img-wrapper">
                                    <img :src="scope.row.cover" alt="" class="info-image" />
                                </div>
                                <div class="info-detail">
                                    <div class="title">
                                        <span>{{ scope.row.title }}</span>
                                    </div>
                                    <div class="price">¥ {{ scope.row.price }}</div>
                                </div>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="tuijian_time" label="加入推荐时间"></el-table-column>
                    <el-table-column label="类型">
                        <template slot-scope="scope">
                            <span>{{
                                scope.row.type == 'image_text'
                                    ? '图文'
                                    : scope.row.type == 'audio'
                                    ? '音频'
                                    : scope.row.type == 'video'
                                    ? '视频'
                                    : '专栏'
                            }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="" label="状态">已推荐</el-table-column>
                    <el-table-column label="操作" align="center">
                        <template slot-scope="scope">
                            <el-row>
                                <el-button @click="deleteRow(scope.row.id, scope.row.type_, scope.$index)" type="danger" size="mini"
                                    >删除</el-button
                                >
                            </el-row>
                        </template>
                    </el-table-column>
                </el-table>
                <!-- <div class="pagination">
                    <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.page"
                        :page-size="query.rows"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                    ></el-pagination>
                </div> -->
            </div>
        </div>
        <!-- 选择产品类型 -->
        <el-dialog width="30%" title="选择产品类型" :visible.sync="productTypeVisible">
            <div class="product_type">
                <span v-for="item in productType" :key="item.id" @click="chooseType(item.value)">
                    {{ item.label }}
                </span>
            </div>
        </el-dialog>
        <!-- 打开内层弹框 -->
        <el-dialog width="36%" :title="`选择${productTypeTitle}`" @close="exit" :visible.sync="innerVisible" append-to-body>
            <el-form ref="form" :model="form" label-width="100px">
                <el-form-item>
                    <el-input v-model="query.keyword" :placeholder="`输入${productTypeTitle}名称`" class="handle-input mr10"></el-input>
                    <el-button type="primary" icon="el-icon-search" @click="courseSearch">搜索</el-button>
                </el-form-item>
                <el-table
                    :data="chooseCourseTableData"
                    @row-click="chooseRow"
                    class="chooseCourseTable"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                >
                    <el-table-column :label="`${productTypeTitle}封面`">
                        <template slot-scope="scope">
                            <img :src="scope.row.cover" alt="" class="info-image" />
                        </template>
                    </el-table-column>
                    <el-table-column prop="title" :label="`${productTypeTitle}名称`"></el-table-column>
                    <el-table-column label="免费/付费">
                        <template slot-scope="scope">
                            <span style="color: #e65c5c">{{ scope.row.price == 0 ? '免费' : '¥ ' + scope.row.price }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="type" :label="`${productTypeTitle}类型`" v-if="productTypeTitle == '课程'">
                        <template slot-scope="scope">
                            {{ scope.row.type == 'video' ? '视频' : scope.row.type == 'audio' ? '音频' : '图文' }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="status" label="状态">
                        <template slot-scope="scope">
                            {{ scope.row.status == 0 ? '未上架' : '已上架' }}
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagination">
                    <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.page"
                        :page-size="query.rows"
                        :total="courseListTotal"
                        @current-change="coursePageChange"
                    ></el-pagination>
                </div>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="exit">关 闭</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import {
    goodsRecommendListData,
    videoListData,
    columnListData,
    examListData,
    addGoodsRecommendData,
    delGoodsRecommendData
} from '../../api/index';
export default {
    // name: 'goodsRecommend',
    data() {
        return {
            query: {
                is_tuijian: 0,
                page: 1,
                rows: 4,
                keyword: ''
            },
            form: {
                id: '',
                type: ''
            },
            productType: [
                {
                    value: 'image_text',
                    label: '图文'
                },
                {
                    value: 'audio',
                    label: '音频'
                },
                {
                    value: 'video',
                    label: '视频'
                },
                {
                    value: 'column',
                    label: '专栏'
                }
            ],
            tableData: [],
            chooseCourseTableData: [],
            productTypeTitle: '',
            switch_recommend: true,
            productTypeVisible: false,
            innerVisible: false,
            pageTotal: 0,
            courseListTotal: 0
        };
    },
    created() {
        this.getData();
    },
    methods: {
        enable($event) {
            $event == false ? this.$message.error('关闭推荐成功!') : this.$message.success('开启推荐成功!');
        },
        getData() {
            goodsRecommendListData(this.query).then((res) => {
                this.tableData = res.data.list;
                this.pageTotal = res.data.count;
            });
        },
        // 搜索
        handleSearch() {
            this.$set(this.query);
            this.getData();
        },
        // 分页
        handlePageChange(val) {
            this.$set(this.query, 'page', val);
            this.getData();
        },
        //新增
        addDialog() {
            this.productTypeVisible = true;
        },
        // 课程列表
        chooseType(type) {
            this.innerVisible = true;
            this.query.page = 1;
            this.query.keyword = '';
            this.query.type=type
            if (type == 'column') {
                this.productTypeTitle = '专栏';
                columnListData(this.query).then((res) => {
                    this.chooseCourseTableData = res.data.list;
                    this.courseListTotal = res.data.count;
                });
               
            } else {
                this.productTypeTitle = '课程';
                console.log(this.query)
                videoListData(this.query).then((res) => {
                    this.chooseCourseTableData = res.data.list;
                    this.courseListTotal = res.data.count;
                });
            }
        },
        // 课程搜索
        courseSearch() {
            if (this.productTypeTitle == '课程') {
                videoListData(this.query).then((res) => {
                    this.chooseCourseTableData = res.data.list;
                    this.courseListTotal = res.data.count;
                });
            } else if (this.productTypeTitle == '专栏') {
                columnListData(this.query).then((res) => {
                    this.chooseCourseTableData = res.data.list;
                    this.courseListTotal = res.data.count;
                });
            } else {
                examListData(this.query).then((res) => {
                    this.chooseCourseTableData = res.data.list;
                    this.courseListTotal = res.data.count;
                });
            }
        },
        // 课程分页
        coursePageChange(val) {
            this.$set(this.query, 'page', val);
            this.courseSearch();
        },
        chooseRow(row) {
            this.$confirm('确定添加该商品吗？', '提示', {
                type: 'warning'
            }).then(() => {
                this.form.id = row.id;
                if (row.type == 'column') {
                    this.form.type = 'column';
                } else {
                    this.form.type = 'knowledge';
                }
                addGoodsRecommendData(this.form).then((res) => {
                    if (res.code == 1) {
                        this.innerVisible = false;
                        this.productTypeVisible = false;
                        this.$message.success('选择商品成功!');
                        this.getData();
                    }
                });
            });
        },
        // 关闭
        exit() {
            this.innerVisible = false;
        },
        //删除
        deleteRow(id, type, index) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                delGoodsRecommendData({ id: id, type: type }).then((res) => {
                    if (res.code == 1) {
                        this.$message.success('删除成功');
                        this.tableData.splice(index, 1);
                        this.getData();
                    }
                });
            });
        }
    }
};
</script>

<style scoped>
.re-setting {
    margin: 20px;
    padding: 20px;
    background-color: white;
}
.box-top-title {
    line-height: 1;
    color: #353535;
    font-weight: 600;
    font-size: 16px;
    padding-left: 8px;
    margin-bottom: 10px;
    border-left: 2px solid #2a75ed;
}
.settings-content {
    display: flex;
    line-height: 36px;
}
.settings-content span {
    font-size: 13px;
}
.settings-content .el-switch {
    margin-left: 30px;
    margin-top: 7px;
}
.settings-content .el-button {
    position: relative;
    left: 50%;
    text-decoration: underline;
}
.re-setting .title_detail {
    color: #969daa;
    font-size: 13px;
    margin-left: 20px;
}
.operation {
    margin-top: 30px;
}
.handle-box {
    margin-bottom: 20px;
    float: right;
}
.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.mr10 {
    margin-right: 10px;
}

/* 授权课程start */
.course-info-wrapper {
    display: flex;
    align-items: center;
    width: 100%;
    height: 64px;
    text-align: left;
    cursor: pointer;
}
.course-info-wrapper .img-wrapper {
    position: relative;
}
.info-image {
    height: 64px;
    width: 80px;
    border-radius: 4px;
}
.info-image-mini {
    height: 60px;
    width: 60px;
    border-radius: 50%;
}
.red {
    color: #ea644a;
}
.course-info-wrapper .info-detail {
    line-height: 1.5;
    height: 100%;
    margin-left: 8px;
}
.course-info-wrapper .info-detail .title {
    max-width: 100%;
    height: 44px;
    font-size: 14px;
    font-weight: 400;
    color: #333333;
    line-height: 22px;
    overflow: hidden;
    text-overflow: ellipsis;
    word-wrap: break-word;
    white-space: pre-wrap;
}
.course-info-wrapper .info-detail .price {
    font-size: 14px;
    font-weight: 400;
    color: #e65c5c;
}
.product_type {
    text-align: center;
}
.product_type span {
    cursor: pointer;
    display: inline-block;
    padding: 0 5px;
    margin: 0 20px;
    color: #fff;
    background: rgba(0, 0, 0, 0.5);
    border-radius: 3px;
}
.chooseCourseTable {
    cursor: pointer;
}
/* 授权课程end */
</style>